import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../config/Config.dart';
import '../services/ScreenAdapter.dart';
import '../services/Storage.dart';
import '../widget/JdButton.dart';
import '../widget/JdText.dart';
import '../widget/MySnackbar.dart';
import 'tabs/Tabs.dart';

class RegisterThirdPage extends StatefulWidget {
  const RegisterThirdPage({super.key});

  @override
  State<RegisterThirdPage> createState() => _RegisterThirdPageState();
}

class _RegisterThirdPageState extends State<RegisterThirdPage> {
  late String tel;
  late String code;
  String password = '';
  String rpassword = '';

  @override
  void initState() {
    super.initState();
    tel = Get.arguments["tel"];
    code = Get.arguments["code"];
  }

  //注册
  doRegister() async {
    if (password.length < 6) {
      showSnackbar(message: "密码长度不能小于6位");
    } else if (rpassword != password) {
      showSnackbar(message: "密码和确认密码不一致");
    } else {
      // var api = '${Config.domain}api/register';
      // var response = await Dio()
      //     .post(api, data: {"tel": tel, "code": code, "password": password});

      // if (response.data["success"]) {
      //   //保存用户信息
      //   Storage.setString('userInfo', json.encode(response.data["userinfo"]));

      //   //返回到根
      //   Get.offAll(const Tabs());
      // } else {
      //   showSnackbar(message: "${response.data["message"]}");
      // }

      //保存用户信息
      Storage.setString(
          'userInfo',
          json.encode([
            {"username": Get.arguments["tel"], "password": password}
          ]));
      Get.toNamed("/login");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("用户注册-第三步"),
      ),
      body: Container(
        padding: EdgeInsets.all(ScreenAdapter.width(20)),
        child: ListView(
          children: [
            const SizedBox(height: 50),
            JdText(
              text: "请输入密码",
              password: true,
              onChanged: (value) {
                password = value;
              },
            ),
            const SizedBox(height: 10),
            JdText(
              text: "请输入确认密码",
              password: true,
              onChanged: (value) {
                rpassword = value;
              },
            ),
            const SizedBox(height: 20),
            JdButton(
              text: "登录",
              color: Colors.red,
              height: 74,
              cb: doRegister,
            )
          ],
        ),
      ),
    );
  }
}
